package com.zyf.mapreduce.combineTextInputforamt;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author Malegod_xiaofei
 * @create 2024-07-21-17:22
 * KEYIN, map 阶段输入的 key 的类型：LongWritable
 * VALUEIN, map 阶段输入的 value 类型：Text
 * KEYOUT, map 阶段输出的 key 类型： Text
 * VALUEOUT map 阶段输出的 value 类型： IntWritable
 */
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private Text outK = new Text();
    private IntWritable outV = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        // 1 获取一行
        // atguigu atguigu
        String line = value.toString();

        // 2 切割
        // atguigu
        // atguigu
        String[] words = line.split(" ");

        // 3 循环写出
        for (String word : words) {

            // 封装 MapKey
            outK.set(word);

            // 写出
            context.write(outK, outV);
        }
    }
}
